package com.szcinda.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.szcinda.controller.configuration.UserLoginToken;
import com.szcinda.repository.LinePrice;
import com.szcinda.service.PageResult;
import com.szcinda.service.dto.linePrice.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("linePrice")
public class LinePriceController {

    private final LinePriceService linePriceService;

    public LinePriceController(LinePriceService linePriceService) {
        this.linePriceService = linePriceService;
    }

    @UserLoginToken
    @PostMapping("create")
    @OperationLogAnnotation(operDesc = "新增线路价格", operModel = "线路价格")
    public Result<String> create(@RequestBody LinePriceCreateDto createDto) {
        linePriceService.create(createDto);
        return Result.success();
    }

    @PostMapping("/lineImport/{belongId}/{opId}/{organizationId}")
    @OperationLogAnnotation(operDesc = "导入线路价格", operModel = "线路价格")
    public Result<String> orderImport(@RequestParam("file") MultipartFile file, @PathVariable String belongId, @PathVariable String opId, @PathVariable String organizationId, @RequestParam("feeType") String feeType) throws Exception {
        // 解析每行结果在listener中处理
        InputStream inputStream = file.getInputStream();
        try {
            ExcelListener listener = new ExcelListener();
            EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1, LinePriceImportDto.class), listener);
            linePriceService.batchCreate(listener.getImportDatas(), belongId, opId, organizationId, feeType);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("importLineInCreateModal")
    @OperationLogAnnotation(operDesc = "导入线路价格", operModel = "线路价格")
    public Result<List<LinePriceImportDto>> importLineInCreateModal(@RequestParam("file") MultipartFile file) throws Exception {
        // 解析每行结果在listener中处理
        InputStream inputStream = file.getInputStream();
        ExcelListener listener = new ExcelListener();
        EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1, LinePriceImportDto.class), listener);
        return Result.success(listener.getImportDatas());
    }


    @UserLoginToken
    @PostMapping("modify")
    @OperationLogAnnotation(operDesc = "修改线路价格", operModel = "线路价格")
    public Result<String> modify(@RequestBody LinePriceUpdateDto updateDto) {
        linePriceService.update(updateDto);
        return Result.success();
    }


    @GetMapping("del/{id}/{userId}")
    @OperationLogAnnotation(operDesc = "删除线路价格", operModel = "线路价格")
    public Result<String> delete(@PathVariable String id, @PathVariable String userId) {
        linePriceService.delete(id, userId);
        return Result.success();
    }


    @PostMapping("query")
    public PageResult<LinePrice> query(@RequestBody LinePriceQueryDto queryDto) {
        return linePriceService.query(queryDto);
    }
}
